package deployr.cli;

import static deployr.core.Files.*;
import static deployr.core.Streams.*;

import java.io.*;

import deployr.core.*;

public class ServerCommand extends Command {

	private DeployrServer server = new DeployrServer();

	public ServerCommand(Command other) {
		super(other);
	}

	public ServerCommand(DeployrServer deployrServer) {
		super(null);
		server = deployrServer;
	}

	@Override
	public int onRun(OutputStream output, String[] rawArgs) {
		Args args = new Args(rawArgs);
		int port = args.getParameter("p").orElse(8089);
		String host = args.getParameter("h").orElse("localhost");
		String jettyDir = args.getParameter("d").orElse(userDir(""));
		String webappsDir = args.getParameter("w").orElse(userDir("workspaces/user/webapps"));
		server.run(host, port, file(jettyDir), file(webappsDir));
		server.loop();
		return 0;
	}

	@Override
	protected boolean shouldProcess(String[] args) {
		return args.length > 0 && args[0].startsWith("server");
	}
}
